﻿using System;
using System.ServiceModel.DomainServices.Client;
using RIABlog.Web;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;

namespace RIABlog
{
    /// <summary>
    /// Main <see cref="Application"/> class.
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Глобальный экземпляр контекста доступа к данным
        /// </summary>
        public RIABlogContext BlogContext { get; private set; }

        /// <summary>
        /// Задизаблить все окно
        /// </summary>
        /// <param name="message"></param>
        public void SetIsBusy(string message)
        {
            var busyIndicator = this.RootVisual as RIABlog.Controls.BusyIndicator;

            if (busyIndicator == null)
            {
                return;
            }

            busyIndicator.BusyContent = message;
            busyIndicator.IsBusy = true;
        }

        /// <summary>
        /// Раздизаблить все окно
        /// </summary>
        public void ClearIsBusy()
        {
            var busyIndicator = this.RootVisual as RIABlog.Controls.BusyIndicator;

            if (busyIndicator == null)
            {
                return;
            }

            busyIndicator.IsBusy = false;
        }

        /// <summary>
        /// Creates a new <see cref="App"/> instance.
        /// </summary>
        public App()
        {
            InitializeComponent();

            // Create a WebContext and add it to the ApplicationLifetimeObjects collection.
            // This will then be available as WebContext.Current.
            WebContext webContext = new WebContext();
            webContext.Authentication = new FormsAuthentication();
            this.ApplicationLifetimeObjects.Add(webContext);
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // This will enable you to bind controls in XAML to WebContext.Current properties.
            this.Resources.Add("WebContext", WebContext.Current);

            // This will automatically authenticate a user when using Windows authentication or when the user chose "Keep me signed in" on a previous login attempt.
            WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);

            // Создаем общий экземпляр контекста для всего приложения
            this.BlogContext = new RIABlogContext();

            // Делаем его доступным для байндинга
            this.Resources.Add("BlogContext", this.BlogContext);

            // Show some UI to the user while LoadUser is in progress
            this.InitializeRootVisual();

            // Сразу запускаем загрузку списка блогов, а также команд
			// Используем хэлпер для параллельной загрузки нескольких коллекций
            this.BlogContext.Load
				(
					new EntityQuery[]
					{
						this.BlogContext.GetBlogsQuery(),
                        this.BlogContext.GetCommandsQuery()
					}
				);

			// Определяем, что должно происходить при успешном выполнении команды
        	this.BlogContext.OnCommandExecuted += (op) =>
        		{
					if (!op.HasError)
					{
						MessageBox.Show(op.Value);
					}
        		};
        }

        /// <summary>
        /// Invoked when the <see cref="LoadUserOperation"/> completes.
        /// Use this event handler to switch from the "loading UI" you created in <see cref="InitializeRootVisual"/> to the "application UI".
        /// </summary>
        private void Application_UserLoaded(LoadUserOperation operation)
        {
            if (operation.HasError)
            {
                ErrorWindow.CreateNew(operation.Error);
                operation.MarkErrorAsHandled();
            }
        }

        /// <summary>
        /// Initializes the <see cref="Application.RootVisual"/> property.
        /// The initial UI will be displayed before the LoadUser operation has completed.
        /// The LoadUser operation will cause user to be logged in automatically if using Windows authentication or if the user had selected the "Keep me signed in" option on a previous login.
        /// </summary>
        protected virtual void InitializeRootVisual()
        {
            RIABlog.Controls.BusyIndicator busyIndicator = new RIABlog.Controls.BusyIndicator();
            busyIndicator.Content = new MainPage();
            busyIndicator.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            busyIndicator.VerticalContentAlignment = VerticalAlignment.Stretch;

            this.RootVisual = busyIndicator;
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using a ChildWindow control.
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                // NOTE: This will allow the application to continue running after an exception has been thrown but not handled. 
                // For production applications this error handling should be replaced with something that will report the error to the website and stop the application.
                e.Handled = true;
                ErrorWindow.CreateNew(e.ExceptionObject);
            }
        }
    }
}